package com.china08.yxyapi.api.home.activity.healthcompetition.tongjipage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;

/**
 * 健康知识竞赛 根据citycode查询区县集合
 * 
 * @author zd.Jin
 *
 *
 */
@CrossOrigin
@RestController
public class AreaLstByCitycodeController extends YxyApiBaseController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${yx.rest.api.yx.path}")
    private String yxpath;

    @RequestMapping(value = "/api_yw/home/activity/healthcompetition/huodongtongji/arealst/bycitycode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        List<Area> districts = new ArrayList<>();
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
            Map<String, Object> formMap = new HashMap<String, Object>();
            formMap.put("id", reqModel.citycode);
            HttpEntity<Map<String, Object>> formEntity = new HttpEntity<Map<String, Object>>(formMap, headers);
            Districts httpResult = restTemplate.postForObject(yxpath + "/comm/area/pid", formEntity, Districts.class);
            districts = httpResult.areas;
        } catch (Exception e) {
            e.printStackTrace();
        }
        respModel.areas = districts;
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 市编码
        public String citycode = "230100";
    }

    protected static class RespModel extends YxyApiRespModel {
        // 区县集合
        public List<Area> areas;
    }

    protected static class Districts {
        public List<Area> areas;
    }

    protected static class Area {
        // 区县id
        public String id;
        // 区县name
        public String name;
    }

}
